package com.balabala.gym_system.controller.admin;

import com.balabala.gym_enum.ResultEnum;
import com.balabala.gym_system.pojo.dto.*;
import com.balabala.gym_system.pojo.vo.CoachQueryVo;
import com.balabala.gym_system.pojo.vo.CoachVo;
import com.balabala.gym_system.service.CoachService;
import com.balabala.gym_system.swagger.utils.WebUtils;
import com.balabala.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author:zl
 * Date:2023/5/2 13:57
 */
@RequestMapping("/admin/coach")
@RestController
@Api(tags = "教练管理接口")
@CrossOrigin
public class CoachController {
    @Autowired
    private CoachService coachService;

    @PostMapping(value = "/list")
    @ApiOperation(value = "条件查询教练")
    public Result list(@RequestBody CoachQueryDto coachQueryDto) {
        //1.构建分页条件
        PageHelper.startPage(coachQueryDto.getPage(), coachQueryDto.getLimit());
        List<CoachQueryVo> coachList = coachService.findCoachById(coachQueryDto);
        if(coachList==null){
            //return Result.ok(ResultEnum.);
        }
        PageInfo<CoachQueryVo> pageInfo = new PageInfo<>(coachList);

        return Result.ok(coachList, pageInfo.getTotal());

    }

    @GetMapping(value = "/findAll")
    @ApiOperation(value = "查询所有教练")
    public Result findAll() {
        return Result.ok(ResultEnum.GENERALLY_SUCCESS, coachService.findAllName());
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加教练")
    public Result add(@RequestBody @Validated CoachDto coachDto, BindingResult bindingResult) {
        //判断校验是否通过
        if (bindingResult.hasErrors()) {
            return WebUtils.getResult(bindingResult);
        }
        return coachService.addCoach(coachDto);
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改教练")
    public Result update(@RequestBody CoachUpdateDto coachUpdateDto) {
        return Result.judge(coachService.updateById(coachUpdateDto));
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    @ApiOperation(value = "删除教练")
    public Result deleteById(@RequestParam Long id) {
        return Result.judge(coachService.removeById(id));
    }

    @GetMapping(value = "/findById/{id}")
    @ApiOperation(value = "根据id查找教练信息")
    public Result findById(@PathVariable("id")  Long id) {
        CoachVo byId = coachService.findById(id);
        return  Result.ok(byId);
    }


}
